package cn.dyp.com.splashandguide.activity;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.FrameLayout.LayoutParams;

import java.util.ArrayList;
import java.util.List;

import cn.dyp.com.splashandguide.R;
import cn.dyp.com.splashandguide.adaper.ViewPagerAdapter;

/**
 * Created by lenovo on 2017/5/20.
 */

public class GuideActivity extends BaseActivity implements ViewPager.OnPageChangeListener {

    private ViewPager mViewPager;
    private ViewPagerAdapter mAdapter;
    private List<Integer> mPictureList;//Integer整数类型
    private LinearLayout mPointSetLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.guide_show);
        initData();
        initView();
        initPoint();
        setViewPager();
    }

    //初始化数据添加图片资源
    private void initData() {
        mPictureList = new ArrayList<Integer>();//初始化数组
        mPictureList.add(R.drawable.main_bg);
        mPictureList.add(R.drawable.history_bg);
        mPictureList.add(R.drawable.control_bg);
    }

    //初始化控件
    private void initView() {
        mPointSetLayout = (LinearLayout) findViewById(R.id.point_show_layout);
        mViewPager = (ViewPager) findViewById(R.id.picture_show_vp);
    }

    //为ViewPager设置相应的参数
    private void setViewPager() {
        mAdapter = new ViewPagerAdapter(this, mPictureList);
        mViewPager.setAdapter(mAdapter);
        mViewPager.addOnPageChangeListener(this);
        mViewPager.setCurrentItem(0);
    }

    //初始化界面下面指示器的图片
    private void initPoint() {
        // 清除所有的视图
        mPointSetLayout.removeAllViews();

        for (int i = 0; i < mPictureList.size(); i++) {
            ImageView mImageView = new ImageView(this);
            if (i == 0) {
                mImageView.setBackgroundResource(R.drawable.page_indicator_focused);
            } else {
                mImageView.setBackgroundResource(R.drawable.page_indicator_unfocused);
            }

            mImageView.setLayoutParams(new LayoutParams(20, 20, Gravity.CENTER));
            //LayoutParams类是用于child view（子视图） 向 parent view（父视图）传达自己的意愿的一个东西（孩子想变成什么样向其父亲说明）
            LayoutParams mParams = new LayoutParams(new ViewGroup.LayoutParams(50, 50));
            mParams.leftMargin = 50;
            mParams.rightMargin = 50;
            mPointSetLayout.addView(mImageView, mParams);
        }
    }


    //滚动状态发生变化时调用
    @Override
    public void onPageScrollStateChanged(int arg0) {
    }

    //当前页滚动时调用(不论是人为的触摸滚动还是程序自己执行的滚动)
    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
    }

    //新页面选中时调用
    @Override
    public void onPageSelected(int arg0) {
        selectPointShow(arg0);
    }
    //选择显示的点
    private void selectPointShow(int index) {
        for (int i = 0; i < mPointSetLayout.getChildCount(); i++) {
            ImageView mImageView = (ImageView) mPointSetLayout.getChildAt(i);
            if (i == index) {
                mImageView.setBackgroundResource(R.drawable.page_indicator_focused);
            } else {
                mImageView.setBackgroundResource(R.drawable.page_indicator_unfocused);
            }
        }
    }
}
